package com.eedi.framework.sms.controller.sys;

import com.eedi.framework.common.pojo.CommonResult;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.common.util.object.BeanUtils;
import com.eedi.framework.common.validation.AddGroup;
import com.eedi.framework.common.validation.UpdateGroup;
import com.eedi.framework.sms.controller.sys.vo.template.SysSmsTemplateResp;
import com.eedi.framework.sms.dal.dataobject.SysSmsTemplateDO;
import com.eedi.framework.sms.controller.sys.vo.template.SysSmsTemplatePageReq;
import com.eedi.framework.sms.controller.sys.vo.template.SysSmsTemplateSaveReq;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.eedi.framework.sms.service.SmsTemplateService;
import javax.annotation.Resource;
import javax.validation.Valid;

import static com.eedi.framework.common.pojo.CommonResult.success;


@Tag(name = "系统界面 - 短信模板")
@RestController
@RequestMapping("/sms-template")
@Validated
public class SysSmsTemplateController {

    @Resource
    private SmsTemplateService systemSmsTemplateService;

    @PostMapping("/create")
    @Operation(summary = "创建短信模板")
 //   @PreAuthorize("@ss.hasPermission('member:sms-template:create')")
    public CommonResult<String> createSmsTemplate(@Validated({AddGroup.class}) @RequestBody SysSmsTemplateSaveReq createReqVO) {
        return success(systemSmsTemplateService.createSmsTemplate(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新短信模板")
 //   @PreAuthorize("@ss.hasPermission('member:sms-template:update')")
    public CommonResult<Boolean> updateSmsTemplate(@Validated({UpdateGroup.class}) @RequestBody SysSmsTemplateSaveReq updateReqVO) {
        systemSmsTemplateService.updateSmsTemplate(updateReqVO);
        return success(true);
    }

    @GetMapping("/delete")
    @Operation(summary = "删除短信模板")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:sms-template:delete')")
    public CommonResult<Boolean> deleteSmsTemplate(String systemSmsTemplateId) {
        systemSmsTemplateService.deleteSmsTemplate(systemSmsTemplateId);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得短信模板")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:sms-template:query')")
    public CommonResult<SysSmsTemplateResp> getSmsTemplat(String systemSmsTemplateId) {
        SysSmsTemplateDO smsTemplate = systemSmsTemplateService.getSmsTemplate(systemSmsTemplateId);
        return success(BeanUtils.toBean(smsTemplate, SysSmsTemplateResp.class));
    }

    @PostMapping("/page")
    @Operation(summary = "获得短信模板分页")
    @PreAuthorize("@ss.hasPermission('member:sms-template:query')")
    public CommonResult<PageResult<SysSmsTemplateResp>> getSmsTemplatePage(@RequestBody @Valid SysSmsTemplatePageReq pageReqVO) {
        PageResult<SysSmsTemplateDO> pageResult = systemSmsTemplateService.getSmsTemplatePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SysSmsTemplateResp.class));
    }
}
